package org.example.pims.controller;

import com.tencentcloudapi.captcha.v20190722.CaptchaClient;
import com.tencentcloudapi.captcha.v20190722.models.DescribeCaptchaResultRequest;
import com.tencentcloudapi.captcha.v20190722.models.DescribeCaptchaResultResponse;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import org.example.commons.config.JonProperties;
import org.example.commons.constant.HttpStatusConstant;
import org.example.commons.vo.ResultVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * author 陈伯瑞
 * date 2020/6/10 0010 10:00
 * version 1.0
 **/
@RestController
@RequestMapping("/api/captcha")
public class TCaptchaVerifyController {
    /**
     * 腾讯云票据校验，防水墙api
     */
    @GetMapping("/tencent")
    public ResultVO TCapVerify(@RequestParam("Ticket") String Ticket,
                           @RequestParam("Randstr") String Randstr,
                           @RequestParam("UserIp") String UserIp){
        DescribeCaptchaResultResponse resp;
        try{

            Credential cred = new Credential(JonProperties.getSecretId(), JonProperties.getSecretKey());
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("captcha.tencentcloudapi.com");
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            CaptchaClient client = new CaptchaClient(cred, "ap-guangzhou", clientProfile);

            String params = "{\"CaptchaType\":9,\"Ticket\":\""+Ticket+"\",\"UserIp\":\""+ UserIp+"\",\"Randstr\":\""+Randstr+"\",\"CaptchaAppId\":\""+JonProperties.getCaptchaAppId()+"\",\"AppSecretKey\":\""+JonProperties.getAppSecretKey()+"\"}";
            DescribeCaptchaResultRequest req = DescribeCaptchaResultRequest.fromJsonString(params, DescribeCaptchaResultRequest.class);
            resp = client.DescribeCaptchaResult(req);

            if(resp.getCaptchaCode()<2){
                return  ResultVO.success(resp);
            }

        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
        return ResultVO.error(HttpStatusConstant.UNSUPPORTED_TYPE,"请重新验证!");
}
}

